/**
 * 定义天报表模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.zhuhai.remoteControl.guanLuStatusManager', ['RemoveControlService']);
	
	module.controller('guanLuStatusManager',function($scope,$OrderService){

		$scope.guanluStatusSettingComData = [{"k":"DS_Mode","v":"定时模式"},{"k":"LX_Mode","v":"轮询模式"},{"k":"SD_Mode","v":"手动模式"}];
		$scope.sendOrder = function() {
			$scope.orderItems=[];
/*			if($scope.StartTime1.length>1||$scope.StartTime2.length>1||$scope.StartTime3.length>1) {
				$.messager.alert("提示","请输入整数");
				return false;
			}*/
			var guanluStyle = $scope.guanluStyle;
			if(guanluStyle=="DS_Mode") {
				$scope.orderItems = [
		                               {
		                      			   order_code:'DS_Mode',
		                      			   order_value:'1'
		                               },
		                               {
		                      			   order_code:'LX_Mode',
		                      			   order_value:'0'
		                               },
		                               {
		                      			   order_code:'SD_Mode',
		                      			   order_value:'0'
		                               },
		                               {
		                      			   order_code:'DS_1_Start',
		                      			   order_value:($scope.StartTime1==""||typeof($scope.StartTime1)=="undefined")?'0':$scope.StartTime1
		                               },
		                               {
		                      			   order_code:'DS_2_Start',
		                      			   order_value:($scope.StartTime2==""||typeof($scope.StartTime2)=="undefined")?'0':$scope.StartTime2
		                               },
		                               {
		                      			   order_code:'DS_3_Start',
		                      			   order_value:($scope.StartTime3==""||typeof($scope.StartTime3)=="undefined")?'0':$scope.StartTime3
		                               }
		                               
					     ];
			} else if(guanluStyle=="LX_Mode") {
				$scope.orderItems = [
		                               {
		                      			   order_code:'DS_Mode',
		                      			   order_value:'0'
		                               },
		                               {
		                      			   order_code:'LX_Mode',
		                      			   order_value:'1'
		                               },
		                               {
		                      			   order_code:'SD_Mode',
		                      			   order_value:'0'
		                               },
		                               {
		                      			   order_code:'LX_JG_Set',
		                      			   order_value:($scope.timeSpot==""||typeof($scope.timeSpot)=="undefined")?'0':$scope.timeSpot
		                               }
		                               
					     ];
			} else if(guanluStyle=="SD_Mode") {
				$scope.orderItems = [
		                               {
		                      			   order_code:'DS_Mode',
		                      			   order_value:'0'
		                               },
		                               {
		                      			   order_code:'LX_Mode',
		                      			   order_value:'0'
		                               },
		                               {
		                      			   order_code:'SD_Mode',
		                      			   order_value:'1'
		                               }
		                               
					     ];
			}
			$.messager.confirm("确认","确定要发送命令吗？",function(r){
				if(r){
					$OrderService.sendOrders($scope.openedStationDeviceNum,$scope.orderItems,function(e){
						$.messager.alert("提示","命令已发送");
						$scope.insertLog();
					});
				}
			});
		}
		
		
		/**
		 * 校验各个模式下的必填模式
		 */
		function check() {
			var guanluStyle = $scope.guanluStyle;
			if(guanluStyle=="DS_Mode"){
				if($scope.StartTime1==null ||$scope.StartTime2==null ||$scope.StartTime3==null) {
			    	$.messager.alert("提示","定时模式时间不能为空");
			    	return false;
				}
			} else if(guanluStyle=="LX_Mode") {
				if($scope.timeSpot==undefined){
					$.messager.alert("提示","轮询时间间隔不能为空");
					return false;
				}
			}
			return true;
		}
		
		/**
		  * stationNoToOpen 要开启的站点编号
		 */
		$scope.openStation = function(curRow){
			var stationNoToOpen = curRow.CODE;
			var STATION_STATUS = curRow.STATION_STATUS;
			var NAME = curRow.NAME;
			var MN = curRow.DEVICE_NUM;
			$.messager.confirm("确认","确定要切换管路么？",function(r){
				if(r){
					var queryObj = {
		    				stationNoToOpen:stationNoToOpen,
		    				status:STATION_STATUS,
		    				MN:MN,
		    				NAME:NAME	
		    		};
		    		$waterDataCommon.post("/ZhuhaiMonitorStation/stationOpen.do",queryObj).then(function(result){
		    			$.messager.alert("设置成功","管道将在下一个测量周期切换到“"+NAME+"”","info");
		    		});
				}
			});
		}
		/**
		 * 查询站点
		 */
		$scope.queryStation = function(){
			$scope.queryObj.BUILD_STATUS = '0';
			$waterDataCommon.post("/MonitorStation/getListWithCompany.do",$scope.queryObj).then(function(result){
				var stationList = result.data;
				for(var i=0;i<stationList.length;i++){
					var station = stationList[i];
					var name = station.NAME;
					if(name.indexOf("广昌") > -1 && $scope.userid != 'aomen'){
						stationList[i].canOpen = true;
					}
				}
				$scope.stationList = stationList;
				$wdp._cache_stationList = stationList;
			});
			$waterDataCommon.post("/MonitorStation/getStatusStatistics.do",$scope.queryObj).then(function(result){
				$scope.statusStatistics = result;
				$wdp._cache_statusStatistics = result;
			});
			$waterDataCommon.post("/district/getDistrict.do").then(function(data){
				for(var i=0;i<data.qu.length;i++){
					data.qu[i].v=data.qu[i].NAME;
					data.qu[i].k=data.qu[i].CODE;
				}
				$scope.area = data.qu;
				$wdp._cache_area=data.qu;
			});
		}
		/**
		 * 查询已经开启的站点
		 */
		$scope.getOpenedStationNo = function(){
			$waterDataCommon.post("/ZhuhaiMonitorStation/getOpenedStationNo.do",{}).then(function(result){
				$scope.openedStationNo = result.openedStationNo;
				$scope.openedStationDeviceNum = result.openedStationDeviceNum;
				var queryObj = {
	    				MN:$scope.openedStationDeviceNum
	    		};
				$waterDataCommon.post("/ZhuhaiMonitorStation/getPipeCurrentStyle.do",queryObj).then(function(result){
					$scope.pipeCurrentStr = result.Mode;
					$scope.StartTime1=parseInt(result.start1);
					$scope.StartTime2=parseInt(result.start2);
					$scope.StartTime3=parseInt(result.start3);
					$scope.timeSpot=parseInt(result.lxJG);
					$scope.jianceTime = result.CYECLE;
				});
			});

		}
		/**
		 * 管路状态设置日志记录，定时，手动，轮询
		 */
		$scope.insertLog = function() {
			var guanluStyle = $scope.guanluStyle;
			var StartTime1 = $scope.StartTime1;
			var StartTime2 = $scope.StartTime2;
			var StartTime3 = $scope.StartTime3;
			var MN=$scope.openedStationDeviceNum;
			var queryObj={};
			if(guanluStyle=="DS_Mode") {
				queryObj = {
	    				MN:MN,
	    				OPERATION_ITEM:"设置定时模式："+StartTime1+"点,"+StartTime2+"点,"+StartTime3+"点,"
	    		};
			} else if(guanluStyle=="LX_Mode") {
				queryObj = {
	    				MN:MN,
	    				OPERATION_ITEM:"模式切换：轮询模式，"+$scope.timeSpot+"小时"
	    		};
			} else if(guanluStyle=="SD_Mode") {
				queryObj = {
	    				MN:MN,
	    				OPERATION_ITEM:"模式切换：手动模式"
	    		};
			}
			$waterDataCommon.post("/ZhuhaiMonitorStation/insertSystemLog.do",queryObj).then(function(result){
			});
		}
		$scope.queryStation();
		$scope.getOpenedStationNo();
	});
}(window, angular));